//
//  DDInvitedViewController.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/10/25.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDInvitedViewController.h"
#import "DDInterviewTypeCell.h"
#import "DDDiscoverJobCell.h"

#import "DDIntervitedService.h"

@interface DDInvitedViewController ()

@property(nonatomic,strong)DDIntervitedService *intervitedService;

@property(nonatomic,strong)NSString *interviewSendResult; // 发送面试邀请的结果

@property(nonatomic,strong)NSString *interviewid; // 面试邀请的id

@property(nonatomic,assign)NSInteger interviewTypetoSendMessage; // 面试邀请的id 用于发送自定义消息     面试类型  0:现场面试   1：视频面试

@property(nonatomic,strong)NSString *timeLabelText; // 面试时间

@property(nonatomic,strong)NSString *phoneText; // 面试联系方式

@property(nonatomic,strong)NSString *addressText; // 面试地址

@property(nonatomic,strong)NSString *remarkText; // 面试其他说明

@end

@implementation DDInvitedViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"面试邀请";
    
    self.intervitedService = [[DDIntervitedService alloc]init];
    
    self.heightForinterviewSection = 320;  //第一次默认为到场面试   高度为320   在线面试 高度为200
    
    [self configureTableView];
}

- (void)configureTableView
{
    WEAKSELF
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.mas_equalTo(weakSelf.view);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    DDinterviewHeaderView *headerView = [[DDinterviewHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 85)];
    headerView.nameTitle = self.jobfinderName;
    self.tableView.tableHeaderView = headerView;
}

#pragma mark  UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
  if (indexPath.section == 0){
    
        return 182;
    }else{
    
        return self.heightForinterviewSection;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section) {
        case 0:
        {
            DDDiscoverJobCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DDDiscoverJobCell"];
            if (cell == nil) {
                
                cell = [[[NSBundle mainBundle] loadNibNamed:@"DDDiscoverJobCell" owner:self options:nil] lastObject];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell updateViewWithData:self.jobModel];
            
            return cell;
        
        }
            break;
            
        case 1:
        {
            DDInterviewTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"interviewType"];
            if (cell == nil) {
                
                cell = [[DDInterviewTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"interviewType"];
            }
            
            WEAKSELF
            cell.didGetInterviewType = ^(INTERVIEWTYPE InterviewType){
                /**
                 *  在线面试
                 */
                if (InterviewType == VIDEOINTERVIEW) {
                    
                    weakSelf.heightForinterviewSection = 200;
                    [weakSelf.tableView reloadData];
                }
                /**
                 *  到场面试
                 */
                if (InterviewType == LIVEINTERVIEW) {
                    
                    weakSelf.heightForinterviewSection = 320;
                    [weakSelf.tableView reloadData];
                }
            };
            
            return cell;
        }
            break;
        default:
            break;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 1;
    }else{
        
    return TableViewSectionHeight;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        
        return 100;
    }else{
        
    return 1;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    if (section == 1) {
        
        UIView *sectionFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,100)];
        
        UIButton *interViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
        interViewButton.backgroundColor = COLOR_BUTTON_MAIN_GREEN;
        interViewButton.layer.masksToBounds = YES;
        interViewButton.layer.cornerRadius = 5;
        [interViewButton setTitle:@"发送邀请" forState:UIControlStateNormal];
        [interViewButton addTarget:self action:@selector(interViewAction) forControlEvents:UIControlEventTouchUpInside];
        [sectionFooterView addSubview:interViewButton];
        
        [interViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(sectionFooterView).offset(25);
            make.left.mas_equalTo(sectionFooterView).offset(30);
            make.right.mas_equalTo(sectionFooterView).offset(-30);
            make.height.equalTo(@(45));
        }];
        return sectionFooterView;
    }else{
        
        return nil;
    }
}

// 发送邀请
- (void)interViewAction
{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    DDInterviewTypeCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    self.timeLabelText = cell.interviewTimeLabel.text;
    self.phoneText = cell.fieldPhone.text;
    self.addressText = cell.fieldAddress.text;
    self.remarkText = cell.fieldRemark.text;
  
    WEAKSELF
    /**
     *  在线面试
     */
    if (weakSelf.heightForinterviewSection == 200) {
        
        if (self.timeLabelText.length == 0) {
            
            [weakSelf showLDAlertView:@"请填写面试时间"];
        }else{
        
            /**
             *  在线面试邀请 必填，2在线面试
             */
            self.interviewTypetoSendMessage = 1;
            [self setupDataWithInterviewType:@"2" withInterviewdt:self.timeLabelText withLink_mobile:self.phoneText withAddress:self.addressText withRemark:self.remarkText];
        }
    }else{
      
        /**
         *  到场面试
         */
        if (self.timeLabelText.length == 0) {
            
            [weakSelf showLDAlertView:@"请先填写面试时间"];
        }else{
            
            if (self.phoneText.length == 0) {
                
                [weakSelf showLDAlertView:@"请先填写联系方式"];
            }else{
            
                if (self.addressText.length == 0) {
                    
                    [weakSelf showLDAlertView:@"请填写面试地点"];
                }else{
                   
                    /**
                     *  到场面试邀请 必填，1到场面试
                     */
                    self.interviewTypetoSendMessage = 0;
                    [self setupDataWithInterviewType:@"1" withInterviewdt:self.timeLabelText withLink_mobile:self.phoneText withAddress:self.addressText withRemark:self.remarkText];
                }
            }
        }
    }
}

/**
 *  保存面试
 *
 *  @param interviewType 必填，1到场面试 2在线面试
 *  @param interviewID   纪录id，修改需必填，新建不填
 *  @param interviewdt   必填，约定的面试时间
 *  @param link_mobile   联系方式
 *  @param address       面试地址
 *  @param remark        备注
 *  @param successBlock  <#successBlock description#>
 *  @param failBlock     <#failBlock description#>
 */
- (void)setupDataWithInterviewType:(NSString *)interviewType withInterviewdt:(NSString *)interviewdt withLink_mobile:(NSString *)link_mobile withAddress:(NSString *)address withRemark:(NSString *)remark
{
    
    WEAKSELF
    //state  状态：1　面试发起 2被拒绝  3同意面试 4面试取消 5面试结束 6已经扣费
    [self.intervitedService sendInterviewType:interviewType withState:@"1" withInterViewID:@"" withJob_id:self.job_id withEmplyoer_id:self.emplyoer_id withJobfinder_id:self.jobfinder_id withInterviewdt:interviewdt withLink_mobile:link_mobile withAddress:address withRemark:remark SuccessBlock:^(id responseObject) {
        
        self.interviewSendResult = responseObject[@"msg"];
        
        if ([self.interviewSendResult isEqualToString:@"成功！"]) {
            
            self.interviewid = responseObject[@"data"][@"id"];
        }
        
        [self performSelectorOnMainThread:@selector(backToMainThread) withObject:self waitUntilDone:YES];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf showLDAlertView:@"发送邀请失败"];
    }];
}


- (void)backToMainThread
{
    WEAKSELF
   if ([self.interviewSendResult isEqualToString:@"成功！"]){
       
       /**
        *   点击发送邀请的回调
        */
       if (self.didGetInterviewMessage) {
           
        self.didGetInterviewMessage(self.interviewTypetoSendMessage ,self.interviewid , self.timeLabelText , self.phoneText , self.addressText , self.remarkText);
       };
       
       [self.navigationController popViewControllerAnimated:YES];
    
   }else{
   
       [weakSelf showLDAlertView:@"发送邀请失败"];
   }
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [self.rdv_tabBarController setTabBarHidden:false animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
